import Vue from 'Vue'
import VueResource from 'vue-resource'
Vue.use(VueResource)

function request (url, data, callback,callback2) {
  Vue.http.post(process.env.API_ROOT.concat(url), data).then((response) => {
    var code = response.body.Code
    var msg = response.body.Message
    if (code === 10000) {
      callback(response.body.Result)
    } else {
      alert(msg)
      callback2(msg);
    }
  }, (error) => {
    alert(error)
  })
}

let HTTP = {};
var response = {};
HTTP = async function(url,data) {
  try {
    response = Vue.http.post(process.env.API_ROOT.concat(url),data)
  } catch (error) {
    response = error()
    console.log(error);
  }
  return response;
}

//设置请求拦截器
Vue.http.interceptors.push((request,next) =>{
  request.method = 'POST';
  //设置放行函数
  next((response) =>{
    console.log('请求地址：'+response['url']);
    console.log(response['data']);
    return response;
  })
})

export {HTTP}

export default {
  request: request,
}
